﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Reflection
{
    public static class TypeExtension
    {
        /// <summary>
        /// 动态创建一个类型的对象
        /// </summary>
        /// <param name="t"></param>
        /// <param name="paramas"></param>
        /// <returns></returns>
        public static object CreateInstance(this Type t,object[] paramas)
        {
            int pCount = paramas == null ? 0 : paramas.Length;
            foreach (var item in t.GetConstructors())
            {
                var p = item.GetParameters();
                if (p.Length == pCount)
                    return item.Invoke(paramas);
            }
            throw new InvalidOperationException("没有找到合适的构造函数");
        }
    }
}
